package com.qdxwx.web.controller.user;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 直播接口
 * @author developer
 */
@Controller
public class UserLiveController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(UserLiveController.class);
	@Autowired
	private CourseService courseService;

	/**
	 * 个人中心  我的直播
	 */
	@RequestMapping("/user/live.html")
	public String ucLive(HttpServletRequest request, ModelMap model) {
		//获取登录用户ID
		long userId = LoginUser.getUserId(request);
		if (userId < 1) {
			return VIEW_ERROR_403;
		}
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(3);


		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setUserId(userId);
		courseQuery.setSellType("LIVE");
		courseQuery.setIsOverdue(0);
		int total = courseService.countMyCourse(courseQuery);
		model.put("totalPages", page.getTotalPages(total));
		//查询我的
		List<CourseDTO> courseList = courseService.getMyCourseList(courseQuery, page);
		model.put("courseList", courseList);
		model.put("page", page);
		return getViewPath("ucenter/uc-live");
	}

	/**
	 * 个人中心  我的直播 过期
	 */
	@RequestMapping("/user/live/overdue.html")
	public String ucOverdueLive(HttpServletRequest request, ModelMap model) {
		//获取登录用户ID
		long userId = LoginUser.getUserId(request);
		if (userId < 1) {
			return VIEW_ERROR_403;
		}
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(3);

		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setUserId(userId);
		courseQuery.setSellType("LIVE");
		courseQuery.setIsOverdue(1);
		int total = courseService.countMyCourse(courseQuery);
		model.put("totalPages", page.getTotalPages(total));
		//查询我的
		List<CourseDTO> courseList = courseService.getMyCourseList(courseQuery, page);
		model.put("courseList", courseList);
		model.put("page", page);

		return getViewPath("ucenter/uc-live-overdue");
	}

	/**
	 * 免费直播列表
	 */
	@RequestMapping("/user/live/free.html")
	public String showFreeCourseList(HttpServletRequest request, ModelMap model) {
		long userId = LoginUser.getUserId(request);
		if (userId < 1) {
			return VIEW_ERROR_403;
		}
		// 页面传来的数据放到page中
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(3);
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setSellType("LIVE");//直播类型
		//只查询上架的
		courseQuery.setIsAvailable(1);
		courseQuery.setIsFree(1);//查询免费的课程
		int total = courseService.countMyCourse(courseQuery);
		model.put("totalPages", page.getTotalPages(total));
		// 搜索课程列表
		List<Course> courseList = courseService.getList(courseQuery, page);
		model.put("courseList", courseList);
		model.put("page", page);
		model.put("courseQuery", courseQuery);
		model.put("sellType", courseQuery.getSellType());

		return getViewPath("ucenter/uc-live-free");
	}
}